package br.com.as.conversores;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

import br.com.as.dominio.Fornecedor;
import br.com.as.negocio.FornecedorService;

/**
 * Classe que converte combo box em objeto fornecedor
 * 
 * @author msante
 *
 */
public class FornecedorConverter implements Converter {

	public Object getAsObject(FacesContext context, UIComponent component, String value) throws ConverterException {
		try {
			if (value == null) {
				return null;
			}
			return new FornecedorService().pesquisarPorId(Long.parseLong(value));
		} catch (Exception e) {
			throw new ConverterException(e);
		}
	}

	public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
		if (object == null) {
			return null;
		}
		Fornecedor fornecedor = (Fornecedor) object;
		return fornecedor.getId().toString();
	}
}
